var mongoose = require('./db')



//WJ
/////////接诊人数
const outpatientSchema = new mongoose.Schema({
    name: String,
    regfee: Number,//挂号费
    clinical: Boolean,//待接诊or已接诊
    numtime: Boolean,//初诊or复诊
    exam: Boolean,//是否检查
    examnum: Number,//检查金额
    test: Boolean,//是否检验
    testnum: Number,//检验金额
    reside: Boolean,//是否住院
    date: Date,//日期
})
////////////处方
const prescriptionsSchema = new mongoose.Schema({
    name: String,
    drug: Boolean,//中药or西药
    price: Number,
    date: Date,//日期
})
///手术
const surgerySchema = new mongoose.Schema({
    name: String,
    outp: Boolean,//住院or门诊
    date: Date,//日期
})
///会诊
const groupSchema = new mongoose.Schema({
    name: String,
    grou: Boolean,//线上or线下
    date: Date,//日期
})
const outpatient = mongoose.model('outpatient', outpatientSchema)
const prescription = mongoose.model('prescriptions', prescriptionsSchema)
const surgery = mongoose.model('surgery', surgerySchema)
const group = mongoose.model('group', groupSchema)


//ZT
const UsersSchema = mongoose.Schema({
    username: String,
    password: String,
    iphone: String,
    email: String,
})
const RoleSchema = mongoose.Schema({
    role_name: String,
    description: String,
})
const PermissionSchema = mongoose.Schema({
    permission_name: String,
    description: String,
})
const UserRoleSchema = mongoose.Schema({
    user_id: String,
    role_id: String,
})
const RolePermissionSchema = mongoose.Schema({
    role_id: String,
    permission_id: String,
})
const verificationCodeSchema = new mongoose.Schema({
    phone: {
        type: String,
        required: true,
        unique: true
    },
    code: {
        type: String,
        required: true
    },
    expirationTime: {
        type: Date,
        required: true
    }
});

const VerificationCode = mongoose.model('VerificationCode', verificationCodeSchema);
const User = mongoose.model('Users', UsersSchema)
const Role = mongoose.model('Role', RoleSchema)
const Permission = mongoose.model('Permission', PermissionSchema)
const UserRole = mongoose.model('UserRole', UserRoleSchema)
const RolePermission = mongoose.model('RolePermission', RolePermissionSchema)
//SWQ



//ZLQ
const moment = require('moment'); // 引入 moment.js
//中药类型
const zhongSchema = new mongoose.Schema({
    grug_name: String,//处方名称
    zhong: [
        {
            type: Boolean,//处方类型
            medicine: String,//药品编码
            drug: String,//药品名称
            specification: String,//药品规格
            xi_signetur: String,//西药注意用法
            quantity: String,//发药数量
            drug_price: String,//药品单价
            notes: String,//备注
        }
        
    ],
    permission: {//处方权限
        type: mongoose.Types.ObjectId,
        ref: "permission"
    },
    owner: String,//拥有者
    remark: String,//处方备注
    time: {
        type: Date,
        default: Date(),
    }
})

//西药类型
const xiSchema = new mongoose.Schema({
    grug_name: String,//处方名称
    xi: [//处方类型
        {
            type: Boolean,//处方类型
            medicine: String,//药品编码
            drug: String,//药品名称
            specification: String,//药品规格
            dose: String,//单词剂量
            drug_num: String,//用药次数
            drug_day: String,//用药天数
            zhong_signetur: String,//药品用法
            quantity: String,//发药数量
            drug_price: String,//药品单价
            notes: String,//备注
        }
    ],
    permission: {//处方权限
        type: mongoose.Types.ObjectId,
        ref: "permission"
    },
    owner: String,//拥有者
    remark: String,//处方备注
    time: {
        type: Date,
        default: Date(),
    }
})

//处方权限
const permissionSchema = new mongoose.Schema({
    name: String
 })
// //用药次数
// const drug_numSchema = new mongoose.Schema({
//     name: String
// })
// //中药药品用法
// const zhong_signeturSchema = new mongoose.Schema({
//     name: String
// })
// //西药药品用法
// const xi_signeturSchema = new mongoose.Schema({
//     name: String
// })

//词条管理
const managementSchema = new mongoose.Schema({
    authority:{
        type:mongoose.Types.ObjectId,
        ref:'authorityModel'
    },
    grouping:{
        type:mongoose.Types.ObjectId,
        ref:"grouping"
    },
    entry:String,
    name:String,
    content:String,
    state:{
        type:Boolean,
        default:0
    },
})
//词条权限
const  authoritySchema = new mongoose.Schema({
    name:String
})
//词条分组
const groupingSchema = new mongoose.Schema({
    name:String,
})

//常用诊断管理
const diagnosticSchema = new mongoose.Schema({
    name:String,
    num:String,
    state:{
        type:Boolean,
        default:false
    },
    diagnostic_cate:{
        type:mongoose.Types.ObjectId,
        ref:"diagnostic_cate"
    }
})

//诊断分类
const diagnostic_cateSchema = new mongoose.Schema({
    name:String
})

//字典
const dictionarySchema = new mongoose.Schema({
    num:String,
    name:String,
    state:{
        type:Boolean,
        default:false
    },
    time: {
        type: Date,
        default: Date,
        get: (value) => moment(value).format('YYYY年MM月DD日 HH:mm:ss')  // 格式化日期
    },
    content:{
        type:String,
        default:""
    },
    dictionary_cate:{
        type:mongoose.Types.ObjectId,
        ref:"dictionary_cate"
    }
})

const dictionary_cateSchema = new mongoose.Schema({
    name:String,
})

const parameterSchema = new mongoose.Schema({
    can_num:String,//参数编码
    name:String,//参数名称
    content:String,//参数说明
    value:String,//参数值
    state:Boolean,//状态
    xi_num:{
        type:String,
        default:'A110'
    },//归属系统编码
    ren:{
        type:String,
        default:"张三"
    },//创建人
})
const log_onSchema = new mongoose.Schema({
    name:String,
    time:{
        type:Date,
        default:Date,
    },
    login:String,
    ip:String,
    mac:String
})
const log_onsSchema = new mongoose.Schema({
    name:String,
    time: {
        type: String,  // 存储为字符串
        default: () => moment().format('YYYY年MM月DD日 HH:mm:ss'),  // 格式化日期
    },
    login:String,
    ip:String,
    mac:String
})

//日志管理
// 定义日志模型
const  journalSchema = new mongoose.Schema({
    module:String,//系统模块
    cate:String,//操作类型
    operator: String,//操作人员
    ipAddress: String,//操作地址
    operationTime: Date,//操作时间
    requestUrl: String,//请求地址
    requestMethod: String,//请求方式
    operationMethod: String,//操作方法
    requestParams: Object,//请求参数
    responseTime: Number//消耗时间（毫秒）
  });
  


//中药类型
const zhongModel = mongoose.model('zhong', zhongSchema)
//西药类型
const permissionModel = mongoose.model('permission', permissionSchema, 'permission')
//处方权限
const xiModel = mongoose.model('xi', xiSchema)
// const drug_numModel = mongoose.model('drug_numModel', drug_numSchema, 'drug_numModel')
// const zhong_signeturModel = mongoose.model('zhong_signeturModel', zhong_signeturSchema, 'zhong_signeturModel')
// const xi_signeturModel = mongoose.model('xi_signeturModel', xi_signeturSchema, 'xi_signeturModel')
const managementModel = mongoose.model('managementModel', managementSchema)
const authorityModel = mongoose.model('authorityModel', authoritySchema)
const groupingModel = mongoose.model('grouping', groupingSchema)
const diagnosticModel = mongoose.model('diagnostic',diagnosticSchema)
const diagnostic_cateModel = mongoose.model('diagnostic_cate',diagnostic_cateSchema)
const dictionaryModel = mongoose.model('dictionary',dictionarySchema)
const dictionary_cateModel = mongoose.model("dictionaryCate",dictionary_cateSchema)
const parameterModel = mongoose.model('parameter',parameterSchema)
const log_onModel = mongoose.model('log_on',log_onSchema)
const log_onsModel = mongoose.model('log_ons',log_onsSchema)
const journalModel = mongoose.model('journal', journalSchema);

//CWS
//患者就诊查询表
let patient = mongoose.Schema({
    Medical: String,   //病历号
    Card: String,    //卡号
    Name: String,   //患者姓名
    Sex: String,     //性别
    Type: String,  //证件类型，
    Documents: String,   //证件号
    status: String,  //状态
    Time: String,   //建档时间
})
let patients = mongoose.model("patients", patient)

// 医技报告查询
let doctor = mongoose.Schema({
    Card: String,   //就诊卡号
    Name: String,   //患者姓名
    Type: String,   //申请单类型
    Numbers: String,  //申请单号
    Names: String,   //申请单名称
    Status: String,   //状态
    Times: String,  //申请时间
})
let doctors = mongoose.model("doctors", doctor)

// 门诊处方查询
let OPD = mongoose.Schema({
    opd: String,   //门(就)诊号
    chunum: String,  //处方号
    card: String,  // 就诊卡号
    name: String,  //患者姓名
    room: String,  //开单科室
    doctor: String,  //开单医生
    money: String, //缴费金额
    status: String, //缴费状态
    time: String,  //时间
})

let OPDS = mongoose.model("OPDS", OPD)

// 门诊挂号费用查询
let Money = mongoose.Schema({
    numbers: String,      //门(就)诊号
    cards: String,       //就诊卡号
    name: String,      //患者姓名
    keshi: String,      //挂号科室
    doctor: String,        // 挂号医生
    money: String,      //挂号金额
    status: String,       //挂号状态
    time: String,        //挂号日期
})
let Moneys = mongoose.model("Moneys", Money)
//WHY
//西药列表
let westdrugSchema = mongoose.Schema({
    drugcode: String,//药品编码
    code: String,//药品编码
    name: String,//名称
    size: String,//规格
    package: String,//包装
    chiacode: String,//拼音码
    unit: String,//发药单位
    MSItype: String,//医保类别
    price: Number,//价格
    inventory: Number,//库存
    expiration: Number,//有效期
})
let westdrugModel = mongoose.model('westdrug', westdrugSchema);
//中药列表
let chiadrugSchema = mongoose.Schema({

    drugcode: String,//药品编码
    code: String,//药品编码
    name: String,//名称
    size: String,//规格
    package: String,//包装
    chiacode: String,//拼音码
    unit: String,//发药单位
    MSItype: String,//医保类别
    price: Number,//价格
    inventory: Number,//库存
    expiration: Number,//有效期
})
let chiadrugModel = mongoose.model('chiadrug', chiadrugSchema);
//病历
let recordSchema = mongoose.Schema({
    hospitalName: String,//医院
    hospitalBook: String,//病历类别
    createTime: { type: Date, default: new Date() },//就诊时间
    isBack: { type: Boolean, default: false },//是否复诊
    name: String,//姓名
    sex: String,//性别
    age: Number,//年龄
    phone: String,//电话
    listnum: String,//病历号
    category: String,//科室
    main: { type: String, default: '' },//主述
    now: { type: String, default: '' },//现病史
    back: { type: String, default: '' },//既往史
    deep: { type: String, default: '' },//过敏史
    one: { type: String, default: '' },//体格检查
    two: { type: String, default: '' },//辅助检查
    sure: { type: String, default: '' },//诊断
    food: { type: String, default: '' },//处方
    other: { type: String, default: '' },//其他
    text: { type: String, default: '' },//建议
    createTime: { type: Date, default: new Date() },//门诊日期
    doctor: String,//医师
})
let recordModel = mongoose.model('record', recordSchema);
//处方
let prescriptionSchema = mongoose.Schema({
    diagnosis: String,//诊断
    room: String,//开单科室
    doctor: String,//开单医生
    price: Number,//金额
    psttype: String,//处方种类
    prescription: [Object],
    state: { type: String, default: '0' },//状态
    createTime: { type: Date, default: new Date() },//记录时间
})
let prescriptionModel = mongoose.model('prescription', prescriptionSchema);
//西药处方
let westPSSchema = mongoose.Schema({
    room: String,//取药药房
    PStype: String,//处方类型
    expiration: String,//有效期
    diagnosis: String,//诊断
    code: String,//药品编码
    name: String,//名称
    size: String,//规格
    metering: String,//单词计量
    numberOfTimes: String,//用药次数
    dayTime: Number,//用药天数
    use: String,//用法
    sendNum: String,//发药数量
    price: Number,//单价
    notes: { type: String, default: '' },//备注
    expiration: String,//有效期
    diagnosis: String,//诊断
    metering: String,//单词计量
    numberOfTimes: String,//用药次数
    dayTime: Number,//用药天数
    type: Boolean,//处方类型//
    medicine: String,//药品编码
    drug: String,//药品名称
    specification: String,//药品规格
    xi_signetur: String,//西药注意用法
    quantity: String,//发药数量
    drug_price: String,//药品单价
    notes: String,//备注
})
let westPSModel = mongoose.model('westPS', westPSSchema);
//中药处方
let chiaPSSchema = mongoose.Schema({
    room: String,//取药药房
    PStype: String,//处方类型
    expiration: String,//有效期
    diagnosis: String,//诊断
    metering: String,//中药剂量
    use: String,//中药用法
    isHot: { type: Boolean, default: false },//是否煎药
    code: String,//药品编码
    name: String,//名称
    size: String,//规格
    isEat: String,//药品用法
    sendNum: String,//发药数量
    price: Number,//单价
    notes: { type: String, default: '' },//备注
    type: Boolean,//处方类型//
    medicine: String,//药品编码
    drug: String,//药品名称
    specification: String,//药品规格
    xi_signetur: String,//西药注意用法
    quantity: String,//发药数量
    drug_price: String,//药品单价
    notes: String,//备注
})
let chiaPSModel = mongoose.model('chiaPS', chiaPSSchema);
//检查
let inspectSchema = mongoose.Schema({
    application: String,//申请单
    applicationcontext: [Object],//申请单内容
    room: String,//开单科室
    doctor: String,//开单医生
    price: Number,//金额
    state: { type: String, default: '0' },//状态
    createTime: { type: Date, default: new Date() },//记录时间
})
let inspectModel = mongoose.model('inspect', inspectSchema);
//检查申请单列表
let ISlistSchema = mongoose.Schema({
    context: [Object]
})
let ISlistModel = mongoose.model('ISlist', ISlistSchema);
//检查申请单项目
let MachineSchema = mongoose.Schema({
    name: String,
    tid: String,
})
let MachineModel = mongoose.model('Machine', MachineSchema);
//检查选择项目
let ISselectSchema = mongoose.Schema({
    name: String,//名称
    price: Number,//价格
    isFast: { type: Boolean, default: false },//是否加急
    notes: { type: String, default: '' },//备注
    diagnosis: String,//诊断
})
let ISselectModel = mongoose.model('ISselect', ISselectSchema);
//检验
let testSchema = mongoose.Schema({
    application: String,//申请单
    applicationcontext: [Object],//申请单内容
    room: String,//开单科室
    doctor: String,//开单医生
    price: Number,//金额
    state: { type: String, default: '0' },//状态
    createTime: { type: Date, default: new Date() },//记录时间
})
let testModel = mongoose.model('test', testSchema);
//检验申请单列表
let testListSchema = mongoose.Schema({
    title: String,
    key: String,
    children: [Object]
})
let testListModel = mongoose.model('testList', testListSchema);
//检查选择项目
let TTselectSchema = mongoose.Schema({
    name: String,//名称
    price: Number,//价格
    isFast: { type: Boolean, default: false },//是否加急
    notes: { type: String, default: '' },//备注
    diagnosis: String,//诊断
})
let TTselectModel = mongoose.model('TTselect', TTselectSchema);

module.exports = {
    //wj
    outpatient,
    prescription,
    surgery,
    group,
    //zt
    User,
    Role,
    Permission,
    UserRole,
    RolePermission,
    VerificationCode,

    //swq


    //zlq
    zhongModel,
     xiModel, 
     permissionModel,
    //  drug_numModel,
    //  zhong_signeturModel,
    //  xi_signeturModel,
     managementModel,
     authorityModel,
     groupingModel,
     diagnosticModel,
     diagnostic_cateModel,
     dictionaryModel,
     dictionary_cateModel,
     parameterModel,
     log_onModel,
     log_onsModel,
     journalModel,
    //cws
    patients,
    doctors,
    OPDS,
    Moneys,
    //why
    westdrugModel,
    chiadrugModel,
    recordModel,
    prescriptionModel,
    westPSModel, chiaPSModel,
    inspectModel,
    ISlistModel, MachineModel, ISselectModel,
    testModel,
    testListModel, TTselectModel,




}